const Dictionary = require("../../model/dictionary.model");
const User = require("../../model/user.model");
const Sequelize = require("sequelize");
class DictionaryService {
  async createDictionary(args) {
    const { id, createdAt, is_it_enabled, updatedAt, ...remainingParameters } =
      args;
    const respnse = await Dictionary.create(remainingParameters);
    return respnse.dataValues;
  }
  async getDictionary(args) {
    const respnse = await Dictionary.findOne({
      where: {
        ...args,
      },
    });
    return respnse.dataValues;
  }
  async getAllDictionary(args) {
    const { current = 1, pageSize = 10, ...remainingParameters } = args;
    const offset = (current - 1) * pageSize;
    const whereOptions = {};
    if (remainingParameters.type) {
      whereOptions.type = {
        [Sequelize.Op.like]: `%${remainingParameters.type}%`,
      };
    }
    if (remainingParameters.title) {
      whereOptions.title = {
        [Sequelize.Op.like]: `%${remainingParameters.title}%`,
      };
    }
    const { rows: list, count: total } = await Dictionary.findAndCountAll({
      limit: Number(pageSize),
      offset: offset,
      where: whereOptions,
      order: [["createdAt",'DESC']],
    });
    return {
      current,
      pageSize,
      total,
      list,
    };
  }
  async updateDictionary(args) {
    const { id, createdAt, is_it_enabled, updatedAt, ...remainingParameters } =
      args;
    const respnse = await Dictionary.update(remainingParameters, {
      where: {
        id: args.id,
      },
    });
    console.log(respnse);
    return respnse[0] > 0 ? true : false;
  }
  async deleteDictionary(id) {
    const respnse = await Dictionary.destroy({
      where: {
        id: id,
      },
    });
    console.log(respnse);
    return respnse == 1 ? true : false;
  }
}

module.exports = new DictionaryService();
